package com.fullstack.server.config

import cats.*
import cats.implicits.*
import pureconfig.error.ConfigReaderException
import pureconfig.{ConfigReader, ConfigSource}

import scala.reflect.ClassTag


object syntax {

  extension (source: ConfigSource)
    def loadF[F[_] : MonadThrow, A: ClassTag](using reader: ConfigReader[A]): F[A] =

      source.load[A].pure[F].flatMap {
        case Left(errors) => ConfigReaderException(errors).raiseError
        case Right(value) => value.pure
      }

}
